/*
 * WeeklyRewardResource.cpp
 *
 *  Created on: 2013. 9. 6.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "WeeklyRewardResource.h"
#include "../NUtil/Utils.h"
#include "../engine.h"

namespace ProjectZ { namespace NResource {

WeeklyRewardResource::WeeklyRewardResource()
{
	pWeekRewardAndroid	= new WeeklyRewardData;
	pWeekRewardIOS		= new WeeklyRewardData;
}

WeeklyRewardResource::~WeeklyRewardResource()
{
	delete pWeekRewardAndroid;
	delete pWeekRewardIOS;
}

bool WeeklyRewardResource::LoadResource()
{
	if (false == pWeekRewardAndroid->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pWeekRewardIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

WeeklyRewardData * WeeklyRewardResource::GetFly(User * pUser)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pWeekRewardAndroid;

	case NUtil::MarketType::IOS_KAKAO:
		return pWeekRewardIOS;

	default:
		return NULL;
	}

	return NULL;
}

WeeklyRewardData::Flyweight * WeeklyRewardResource::FindFly(User * pUser, int key)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pWeekRewardAndroid->GetFly(key);

	case NUtil::MarketType::IOS_KAKAO:
		return pWeekRewardIOS->GetFly(key);

	default:
		return NULL;
	}

	return NULL;
}

WeeklyRewardData::~WeeklyRewardData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void WeeklyRewardData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool WeeklyRewardData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_ranking_reward WHERE company=%d AND sale_cd=%d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		int seq, tmpCompany, tmpSaleCode;
		WeeklyRewardData::Flyweight * pFly = new WeeklyRewardData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> seq >> tmpCompany >> tmpSaleCode >> pFly->_TYPE
				>> pFly->_MIN >> pFly->_MAX >> pFly->_REWARD_SHEET_TYPE
				>> pFly->_REWARD_TID >> pFly->_VALUE;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

//		clsFlyTree[pFly->_RANKING] = pFly;
//		clsFlyTree[WeeklyRewardData::Key(pFly->_MIN, pFly->_MAX)] = pFly;
		clsFlyTree[seq] = pFly;
	}

	return true;
}

//WeeklyRewardData::Flyweight * WeeklyRewardData::GetFly(int rank)
//WeeklyRewardData::	Flyweight * WeeklyRewardData::GetFly(WeeklyRewardData::Key & clsKey)
WeeklyRewardData::	Flyweight * WeeklyRewardData::GetFly(int key)
{
//	auto iter = clsFlyTree.find(rank);
//
//	if( iter == clsFlyTree.end() )
//		return NULL;
//	else
//		return iter->second;
//

	auto iter = clsFlyTree.find(key);

	if (iter == clsFlyTree.end())
		return NULL;

	return iter->second;
}

} /* NResource */ } /* ProjectZ */

